<script setup>
import { useUserOut } from '@/store/user';
import {logoutApi} from '@/api/user'
import { storeToRefs } from 'pinia';
import { ref, inject, computed } from 'vue'

const authenticated = inject('authenticated');
const username = inject('currentUsername');

function login() {
  const loc = window.location
  const port = loc.port ? `:${loc.port}` : '';
  let contextPath = loc.pathname;
  if (!contextPath.endsWith('/')) {
    contextPath = `${contextPath}/`;
  }
  loc.href = `//${loc.hostname}${port}${contextPath}oauth2/authorization/oidc`;
}


async function logout() {
  const response = await logoutApi()
  useUserOut().logout()
  console.info(response)
  window.location.href = response.data.logoutUrl;
  const next = response.data?.logoutUrl ?? '/';
  if (router.currentRoute.value.path !== next) {
    await router.push(next);
  }
}

</script>

<template>
  <div>
    <div v-if="authenticated">
      当前登录：{{ username }}, 
      <a href="javascript:void(0)" @click="logout">退出</a>
    </div>
    <div v-else>
      请点击<a href="javascript:void(0)" @click="login">登录</a>
    </div>
  </div>
</template>